Put the Code back to the Main Queue π
Post
Replies
Boosts
Views
Activity
Hello Together,thanks, to think after about that problem.@OOPer: yep, this looks as the only solution for the long run. Kind regardsDWD
Hello Claude,so you see also no way to convert an Image to UIImage...The other way round, there is no problem, I knowKind RegardsDWD
Hello Claude,this was only for demonstration, not for work.but this not the solution, to change the data type from UIImage to Image, because I need it (datatype UIImage) for further procession in an UIKit View (datatype of struct WorkOnPicture).GreedsDWD
Hello Claude,so, I made an Example. The Error occures, wehn teh view "WorkOnPicture" with the Binding will be called. Only the struct ContentView should be modified...import SwiftUI
import Combine
class GetPicture: ObservableObject {//get picture from everywhere
static let shared: GetPicture = GetPicture()
let willChange = PassthroughSubject<Image?, Never>()
@Published var image: Image? = nil {
didSet {
if image != nil {
willChange.send(image)
}
}
}
func get() {
image = Image("ExamplePNG")
}
}
struct WorkOnPicture: View {
@Binding var image: UIImage
var body: some View {
Text("Hello Picture")
}
}
struct ContentView: View {
@State var image: Image? = nil
var body: some View {
VStack {
image?.resizable().frame(width: 100, height: 100)
.contextMenu {
Button(action: { self.workWithPicture() }) {
Text("Edit")
Image(systemName: "pencil")
}.disabled(image == nil)
}
}.onReceive(GetPicture.shared.$image) { image in
self.image = image
}
.onAppear() {
GetPicture().get()
}
}
func workWithPicture() {
WorkOnPicture(image: image) //<- here is the problem
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}Kind regardsDWD
Please format your code!
you give back differend views. try to wrap them up in AnyView( ... ) (is also called type erased wrapper πbe caution, it's performance intensehttps://developer.apple.com/documentation/swiftui/anyviewDWD
try an own view with initializalization initializer, an example can you find thereKind RegadsDWD
Take a look on this threadGreedsDWD
Take the Button insteed. You can also use the List with Navigation Links, but not on a ForEach loop.Kind regardsDWD